package com.bzframework.project.api.web;

import com.bzframework.pro.authorize.adapter.ReactiveServiceCrudControllerAdapter;
import com.bzframework.pro.authorize.annotation.Authorization;
import com.bzframework.pro.authorize.context.ReactiveUserTokenContextHolder;
import com.bzframework.project.api.entity.OpenApiProtocolEntity;
import com.bzframework.project.api.model.request.ProtocolRequest;
import com.bzframework.project.api.service.OpenApiProtocolService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.hswebframework.ezorm.rdb.mapping.defaults.SaveResult;
import org.hswebframework.web.crud.service.ReactiveCrudService;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

/**
 * @Author bingzhong
 * @Date 2025/3/3 14:20
 * @Description
 */
@Tag(name = "openapi-protocol", description = "openapi-protocol")
@RestController
@RequestMapping("/openapi-protocol")
@RequiredArgsConstructor
public class OpenApiProtocolController implements ReactiveServiceCrudControllerAdapter<OpenApiProtocolEntity, String> {

    private final OpenApiProtocolService openApiProtocolService;

    @Override
    public ReactiveCrudService<OpenApiProtocolEntity, String> getService() {
        return openApiProtocolService;
    }

    @PostMapping("/load")
    @Authorization(ignore = true)
    @Operation(summary = "加载 ApiSupport jar包")
    public Mono<SaveResult> loadApiSupportJar(@RequestBody ProtocolRequest protocolRequest) {
        return ReactiveUserTokenContextHolder.get()
                .flatMap(userToken -> openApiProtocolService.loadApiSupportJar(protocolRequest, userToken));
    }

    @DeleteMapping("/remove/{id}")
    @Operation(summary = "根据id删除")
    public Mono<Integer> removeProtocolById(@PathVariable String id) {
        return openApiProtocolService.removeProtocolById(id);
    }

}
